package cn.itcast.xuesheng.service.impl;

import cn.itcast.xuesheng.dao.TopicDao;
import cn.itcast.xuesheng.dao.impl.TopicDaoImpl;
import cn.itcast.xuesheng.domain.PageBean;
import cn.itcast.xuesheng.domain.Topic;
import cn.itcast.xuesheng.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("topicService")
public class TopicServiceImpl implements TopicService {
    //    private TopicDaoImpl topicDao=new TopicDaoImpl();
    @Autowired
    private TopicDao topicDao;

//    public void setTopicDao(TopicDaoImpl topicDao) {
//    }

    @Override
    public PageBean<Topic> pageQuery(int cid, int currentPage, int pageSize, String title) {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
//        TopicDaoImpl topicDao = new TopicDaoImpl();
        PageBean<Topic> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        int totalCount = topicDao.findTotalcount(cid, title);
        pageBean.setTotalCount(totalCount);
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pageBean.setTotalPage(totalPage);
        int start = (currentPage - 1) * pageSize;
        List<Topic> list = topicDao.findpage(cid, start, pageSize, title);
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    public PageBean<Topic> pageQueryForUsername(String username, int currentPage, int pageSize) {
//        TopicDaoImpl topicDao = new TopicDaoImpl();
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        PageBean<Topic> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        int totalCount = topicDao.findTotalcountbyuser(username);
        pageBean.setTotalCount(totalCount);
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pageBean.setTotalPage(totalPage);
        int start = (currentPage - 1) * pageSize;
        List<Topic> list = topicDao.findpagebyuser(username, start, pageSize);
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    public void update(String num, String tid) {
//        TopicDaoImpl topicDao = new TopicDaoImpl();
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        topicDao.update(Integer.parseInt(num), Integer.parseInt(tid));
    }

    @Override
    public List<Topic> queryforxue() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        return topicDao.queryforxue();
    }

    @Override
    public List<Topic> queryforxue2() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        return topicDao.queryforxue2();
    }

    @Override
    public List<Topic> queryfortu() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        return topicDao.queryfortu();
    }

    @Override
    public List<Topic> queryfortu2() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        return topicDao.queryfortu2();
    }

    @Override
    public List<Topic> queryfortnanv() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        return topicDao.queryfortnanv();
    }

    @Override
    public List<Topic> queryforimg() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        return topicDao.queryforimg();
    }

    @Override
    public List<Topic> queryforxue3() {
//        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao", TopicDao.class);
        return topicDao.queryforxue3();
    }

    @Override
    public List<Topic> queryforx4() {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao",TopicDao.class);
        return topicDao.queryforx4();
    }

    @Override
    public void insertcontext(String username, String title, String context, String cid, String cname, String date,String imgcontext) {
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//        topicDao = applicationContext.getBean("topicDao",TopicDao.class);
        topicDao.insertcontext(username, title, context, Integer.parseInt(cid), cname, date,imgcontext);

    }
}
